/******************************************************************************
 *        Copyright (c) 2005 HOPESEMI Technology Inc.
 *  
 *  All rights reserved. HOPESEMI's source code is an unpublished
 *  work and the use of a copyright notice does not imply otherwise.
 *  This source code contains confidential, trade secret material of
 *  HOPESEMI. Any attempt or participation in deciphering, decoding,
 *  reverse engineering or in any way altering the source code is
 *  strictly prohibited, unless the prior written consent of HOPESEMI
 *  is obtained.ok
 *  
 *  Module:        ustc_api.c
 *
 *  Author:        Philip
 *
 *  DESCRIPTION:
 *                 USTC API
 *
 *  Modification history
 *  --------------------
 *  17 Apr. 2009: Philip created on DVD-XPS
 *  --------------------
 *
 ******************************************************************************/

#include "ustc_comdef.h"
#include "ustc_custom.h"
#include "ustc_hif.h"
#include "ustc_api.h"
#include "ustc_cmr_drv.h"

const GS_CHAR ustc_version[]="USTC_ARM7_V01_01";

/******************************************************************************
 * FUNCTION:    ustc_get_version
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
GS_CHAR* ustc_get_version()
{
	return (GS_CHAR*)ustc_version;
}

/******************************************************************************
 * FUNCTION:    ustc_int_status
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
USTC_U16 ustc_int_status()
{
	return ustc_inh(HIF_INT_STATUS);
}

/******************************************************************************
 * FUNCTION:    ustc_int_enable
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 status
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_int_enable(USTC_U16 status)
{
	USTC_U16 mask_bit=0;

	if (status&0x20)
		mask_bit|=0x400;
	mask_bit|=(status<<11);
	
	ustc_reg_clr(HIF_INT_MASK, mask_bit);
}

/******************************************************************************
 * FUNCTION:    ustc_int_disable
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 status
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_int_disable(USTC_U16 status)
{
	USTC_U16 mask_bit=0x3FF;

	if (status&0x20)
		mask_bit|=0x400;
	mask_bit|=(status<<11);

	ustc_reg_set(HIF_INT_MASK, mask_bit);
}

/******************************************************************************
 * FUNCTION:    ustc_int_clear
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 status
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_int_clear(USTC_U16 status)
{
	USTC_U16 hif_status=status&3;
	USTC_U32 module_status;

	if (status&HIF_INT_STA_ERR)
		hif_status&=0x00004;
	
	ustc_outh(HIF_INT_CLEAR, status);

	if (status&HIF_INT_STA_CMIF)
	{
		module_status=USTC_IO_INW(CMR_INT_STATU);
		USTC_IO_OUTW(CMR_INT_STATU,module_status);
	}
}

/******************************************************************************
 * FUNCTION:    ustc_sw_reset
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 module
 *              USTC_U32 during
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_sw_reset(USTC_U16 module, USTC_U32 during)
{
	ustc_reg_set(HIF_SYS_SW_RST, module&0x3F);
	if (module&HIF_RST_CFIFO)
		ustc_reg_set(HIF_SYS_CTRL, 0x220);
	
	ustc_delay(during);

	ustc_reg_clr(HIF_SYS_SW_RST, module&0x3F);
	if (module&HIF_RST_CFIFO)
		ustc_reg_clr(HIF_SYS_CTRL, 0x220);
}

/******************************************************************************
 * FUNCTION:    ustc_sw_reset_enter
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 module
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_sw_reset_enter(USTC_U16 module)
{
	ustc_reg_set(HIF_SYS_SW_RST, module&0x3F);
	if (module&HIF_RST_CFIFO)
		ustc_reg_set(HIF_SYS_CTRL, 0x220);
}

/******************************************************************************
 * FUNCTION:    ustc_sw_reset_exit
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 module
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_sw_reset_exit(USTC_U16 module)
{
	ustc_reg_clr(HIF_SYS_SW_RST, module);
	if (module&HIF_RST_CFIFO)
		ustc_reg_clr(HIF_SYS_CTRL, 0x220);
}

/******************************************************************************
 * FUNCTION:    ustc_cfg_pll
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 pll
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_cfg_pll(USTC_U16 pll)
{
	ustc_outh(HIF_PLL_CFG, pll);
}

/******************************************************************************
 * FUNCTION:    ustc_cfg_cmr_snr_clk
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 pclk_edge
 *              USTC_U16 mclk_div
 *              USTC_U16 pclk_div
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_cfg_cmr_snr_clk(USTC_U16 pclk_edge, USTC_U16 mclk_div, USTC_U16 pclk_div)
{
	ustc_outh(HIF_CMIF_CLK_CTRL, pclk_edge|HIF_CMR_MCLK_OUT);
	ustc_outh(HIF_CMIF_MCLK_SEL, mclk_div);
	ustc_outh(HIF_CMIF_PCLK_SEL, pclk_div);
}


/******************************************************************************
 * FUNCTION:    ustc_cfg_cmr_tv_clk
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 pclk_edge
 *              USTC_U16 mclk_div
 *              USTC_U16 pclk_div
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_cfg_cmr_tv_clk(USTC_U16 pclk_edge, USTC_U16 mclk_div, USTC_U16 pclk_div)
{
	USTC_U16 pedge=(pclk_edge&1)<<3;
	
	ustc_outh(HIF_CMIF_CLK_CTRL, pclk_edge|pedge|HIF_CMR_PCLK_EXT);
	ustc_outh(HIF_CMIF_MCLK_SEL, mclk_div);
	ustc_outh(HIF_CMIF_PCLK_SEL, pclk_div);
}

/******************************************************************************
 * FUNCTION:    ustc_mclk_enable
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_mclk_enable()
{
	ustc_reg_set(HIF_CMIF_CLK_CTRL, 0x10);
}

/******************************************************************************
 * FUNCTION:    ustc_mclk_disable
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_mclk_disable()
{
	ustc_reg_clr(HIF_CMIF_CLK_CTRL, 0x10);
}

/******************************************************************************
 * FUNCTION:    ustc_cfg_yrc_para
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 low
 *              USTC_U16 hig
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_cfg_yrc_para(USTC_U16 low, USTC_U16 hig)
{
	ustc_outh(HIF_YRC_PARA1, low);
	ustc_outh(HIF_YRC_PARA2, hig);
	ustc_reg_set(HIF_SYS_CTRL, 0x40);
}

/******************************************************************************
 * FUNCTION:    ustc_sys_ctrl
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 para
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_sys_ctrl(USTC_U16 para)
{
	ustc_outh(HIF_SYS_CTRL, para);
}

/******************************************************************************
 * FUNCTION:    ustc_clk_enable
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 module
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_clk_enable(USTC_U16 module)
{
	ustc_reg_set(HIF_SYS_CLK_EN, module);
}

/******************************************************************************
 * FUNCTION:    ustc_clk_disable
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              USTC_U16 module
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_clk_disable(USTC_U16 module)
{
	ustc_reg_clr(HIF_SYS_CLK_EN, module);
}

/******************************************************************************
 * FUNCTION:    ustc_cfifo_data_count
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
USTC_U16 ustc_cfifo_data_count()
{
	return 0x1FF&ustc_inh(HIF_CMIF_FIFO_W);
}

/******************************************************************************
 * FUNCTION:    ustc_cfifo_set
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_cfifo_set()
{
	ustc_outh(HIF_CMIF_FIFO_CTRL, HIF_CFIFO_CTRL_SR);
}

/******************************************************************************
 * FUNCTION:    ustc_cfifo_reset
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
void ustc_cfifo_reset()
{
	ustc_outh(HIF_CMIF_FIFO_CTRL, HIF_CFIFO_CTRL_SW);
}

/******************************************************************************
 * FUNCTION:    ustc_get_id
 *
 * DESCRIPTION: 
 *              
 * Inputs:      
 *              None
 * Outputs:     
 *              
 * Return:      
 *              
 * Notes:       
 *              
 ******************************************************************************/
USTC_U16 ustc_get_id()
{
	return ustc_inh(0x44);
}
